import { Graphics, Point, TextStyle } from "pixi.js";
import { PixijsRender } from "../PixijsRender";
import * as PIXI from "pixi.js";

const __point = new Point();

class TextPool {
    private pool: PIXI.Text[] = [];
    private style: PIXI.TextStyle;
    private activeTexts: PIXI.Text[] = [];
    constructor(defaultStyle: PIXI.TextStyle, ) {
        this.style = defaultStyle;
    }

    // 获取文本对象
    get(text: string): PIXI.Text {
        let textObject: PIXI.Text;
        if (this.pool.length > 0) {
            textObject = this.pool.pop()!;
            textObject.text = text; // 更新文本内容
            textObject.visible = true; // 确保文本可见
        } else {
            // 创建新的文本对象
            textObject = new PIXI.Text({text, style: this.style});
        }

        // 添加到活动数组中
        this.activeTexts.push(textObject);
        return textObject;
    }

    // 回收文本对象
    release(textObject: PIXI.Text): void {
        textObject.visible = false; // 隐藏对象
        this.pool.push(textObject);
        const index = this.activeTexts.indexOf(textObject);
        if (index > -1) {
            this.activeTexts.splice(index, 1);
        }

    }

    // 回收所有文本对象
    releaseAll(): void {
        while (this.activeTexts.length > 0) {
            const textObject = this.activeTexts.pop()!;
            textObject.visible = false; // 隐藏对象
            this.pool.push(textObject); // 加入池中
        }
    }
    

    getActiveTexts(): PIXI.Text[] {
        return this.activeTexts;
    }
}

export class CanvasRuler extends Graphics {
    private __render: PixijsRender;
    private __textPool: TextPool;
    
    step: number = 50;
    constructor(render: PixijsRender) {
        super();
        this.__render = render;

        const style = new TextStyle({
            fontFamily: 'Arial',
            fontSize: 28,
            fill: '#A6A6A6',
        });

        this.__textPool = new TextPool(style);
    }

    update(){
        this.__drawRuler();
    }

    private __updateStep(){
        let x = this.__render.viewRoot.scale.x;
        let factor = [1000, 500, 200, 100, 50, 50, 10, 10, 10, 5, 1];
        let scale = [0.1,0.2, 0.5, 1, 2, 3, 4, 5, 8, 10, 100]
        for (let i = 0; i < scale.length; i++) {
            const s = scale[i];
            if(x < s) {
                this.step = factor[i];
                break;
            } 
            
        }
    }
    
    // 创建标尺函数
    private __drawRuler() {
        this.__textPool.releaseAll();
        this.removeChildren();
        this.clear()
        
        let x = this.__render.viewRoot.scale.x;
        let y = this.__render.viewRoot.scale.y;
        
        this.__updateStep();
        let viewroot = this.__render.viewRoot;
        __point.set(0,0);
        viewroot.getGlobalPosition(__point);
        const width = this.__render.app.renderer.width;
        const height = this.__render.app.renderer.height;
        let count = Math.ceil(__point.x/this.step/x);
        for (let i = 0; i < count; i++) {
            let posx = __point.x - this.step*i*x
            this.moveTo(posx, 0 );
            this.lineTo(posx, height);
            this.stroke({ width: 1, color: 0xA6A6A6, alpha: 0.5 });
            this.__drawString(-this.step*i, posx, height); // 绘制数字
            
        }

        count = Math.ceil((width - __point.x)/this.step/x);
        for (let i = 1; i < count; i++) {
            let posx = __point.x + this.step*i*x
            this.moveTo(posx, 0 );
            this.lineTo(posx, height);
            this.stroke({ width: 1, color: 0xA6A6A6, alpha: 0.5 });
            this.__drawString(this.step*i, posx, height); // 绘制数字          
        }

        count = Math.ceil((height - __point.y)/this.step/y);
        for (let i = 0; i < count; i++) {
            let posy = __point.y + this.step*i*y
            this.moveTo(0, posy );
            this.lineTo(width, posy);
            this.stroke({ width: 1, color: 0xA6A6A6, alpha: 0.5 });
            this.__drawString(this.step*i, 0, posy); // 绘制数字          
        }

        count = Math.ceil((__point.y)/this.step/y);
        for (let i = 1; i < count; i++) {
            let posy = __point.y - this.step*i*y
            this.moveTo(0, posy );
            this.lineTo(width, posy);
            this.stroke({ width: 1, color: 0xA6A6A6, alpha: 0.5 });
            this.__drawString(-this.step*i, 0, posy); // 绘制数字          
        }
       
    }

    // 创建文字绘制函数
    private __drawString(text: number, x: number, y: number): void {
        const num = text.toFixed(0)
        const textObject = this.__textPool.get(num);
        textObject.anchor.set(0,1);
        textObject.x = x;
        textObject.y = y;
        textObject.scale.set(0.5);
        this.addChild(textObject);
    }
}